﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Routing;
using System.Web.Security;
using ProjectClass;
using ProjectClass.DAL;
using System.Threading;

namespace ProjectClass
{
    public class Global : HttpApplication
    {
        void Application_Start(object sender, EventArgs e)
        {
            // Code that runs on application startup
            AuthConfig.RegisterOpenAuth();
        }

        void Application_End(object sender, EventArgs e)
        {
            //  Code that runs on application shutdown

        }

        void Application_Error(object sender, EventArgs e)
        {
            // Code that runs when an unhandled error occurs

        }
        protected void Application_AuthenticateRequest(Object sender, EventArgs e)
        {
            HttpCookie formsCookie = Request.Cookies[FormsAuthentication.FormsCookieName];
            if (formsCookie != null)
            {
                FormsAuthenticationTicket auth = FormsAuthentication.Decrypt(formsCookie.Value);
                var principal = new CustomPrincipal(new CustomIdentity(auth.Name));
                Context.User = Thread.CurrentPrincipal = principal;
            }
        }
    }
}
